<%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="com.CGFrame.tree.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%
	String path=request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 %>
<%!
static Hashtable images=new Hashtable();
static Hashtable actions=new Hashtable();
static String script;
static {
    images.put("IMAGE_PLUS","tree/win98/plus1.gif");
    images.put("IMAGE_PLUS_LAST", "tree/win98/plus2.gif");
    images.put("IMAGE_MINUS", "tree/win98/minus1.gif");
    images.put("IMAGE_MINUS_LAST", "tree/win98/minus2.gif");
    images.put("IMAGE_MIDBLK", "tree/win98/midblk.gif");
    images.put("IMAGE_BLANK", "tree/win98/empty.gif");
    images.put("IMAGE_LASTBLK", "tree/win98/lastblk.gif");
    images.put("IMAGE_LINE", "tree/win98/line2.gif");
    images.put("IMAGE_FOLDER", "tree/win98/folderClosed.gif");
    images.put("IMAGES_FOLDER_OPEN","tree/win98/folderOpen.gif");
    StringBuffer sc=new StringBuffer("<script type=javascript>");
    Iterator imgs=images.values().iterator();
    int k=0;
    while(imgs.hasNext()){
      sc.append("var image"+k+"=new Image();");
      sc.append("image"+k+".src="+(String)imgs.next()+";");
      k++;
    }
    sc.append("</script>");
    script=sc.toString();
    actions.put("CLICK_FOLDER","clickFolder");
    actions.put("CLICK_DOCUMENT","clickDocument");
    actions.put("CLICK_FOLDER_IMG","openFolder");
    actions.put("MOUSEOVER","overMouse");
    actions.put("MOUSEOUT","outMouse");
}

  void paintChilds(Iterator childs,Writer w) throws IOException{
    while(childs.hasNext()){
      paintNode((Node)childs.next(),w);
    }
  }

  void paintNode(Node n,Writer w) throws IOException{
    w.write("<table border='0' cellspacing='0' cellpadding='0'><tr>");
    int level=n.getLevel();
    int id=n.getId();
    Node parent=null;
    String name=((File)n.getValue()).getName();
    boolean last=false;
    for(int i=level-1;i>0;i--){
      parent=n.getParent(i);
      last=parent.isLast();
      w.write("<td><img src=\""+(String) images.get(last ?"IMAGE_BLANK":"IMAGE_LINE") +"\" border='0'></td>");
    }
    last=n.isLast();
    if(n.hasChilds()){
      w.write("<td id=\"plus"+ id + "\" style=\"cursor:hand\" onClick=\""+
((String)actions.get("CLICK_FOLDER_IMG")+"(document,"+id+")")+"\"><img src=\"" +
                (String) images.get(last ? "IMAGE_PLUS_LAST" : "IMAGE_PLUS") +
                "\" border=\"0\"></td>");
    }
    else {
      w.write("<td id=\"plus"+ id + "\"><img src=\"" +
              (String) images.get(last ? "IMAGE_MINUS_LAST" : "IMAGE_MINUS") +
              "\" border=\"0\"></td>");
    }
    w.write("<td id=\"f" + id + "\"><img src=\"" +
            (String) images.get("IMAGE_FOLDER") + "\" border=\"0\"></td>");
    if(n.hasChilds()){
      w.write("<td id=\"td" + id + "\" style=\"cursor:hand\" onClick=\""+
(String)actions.get("CLICK_FOLDER")+"(document,"+id+"),"+
(String)actions.get("CLICK_FOLDER_IMG")+"(document,"+id+")"+"\">" +
name + "</td>");
    }
    else{
      w.write("<td id=\"td" + id + "\" style=\"cursor:hand\" onClick=\""+
(String)actions.get("CLICK_FOLDER")+"(document,"+id+")"+"\">" +
  name + "</td>");
    }
    w.write("</tr></table>");
    if (n.hasChilds()) {
      w.write("<div id=\"div" + id + "\" style=\"display:none\">");
      paintChilds(n.getChilds(), w);
      w.write("</div>");
    }
    w.flush();
  }

%>
<html>
<head>
<title>
tree
</title>
<style type="text/css">
td{font:13px/16px;}
A:link {text-decoration: none;font-size: 12px; color: #0000ff}
A:visited {text-decoration: none;font-size: 12px; color: #0000ff}
A:active {text-decoration: none;font-size: 12px}
A:hover {text-decoration: underline;font-size: 12px}
img{vertical-align: bottom;}
</style>
<%=script%>
<script type="text/javascript">
function changeColor(doc,k){
  var old=doc.getElementById("selectedNode").value;
  if(old!=k){
    if(old!=""){
      doc.getElementById("td"+old).style.backgroundColor=doc.thisForm.bgColor.value;
    }
    doc.getElementById("td"+k).style.backgroundColor=doc.thisForm.selectedColor.value;
    doc.thisForm.selectedNode.value=k;
  }
}
function clickDocument(doc,k){
  changeColor(doc,k);
  alert("Click document "+doc.all("td"+k).innerText+".");
}
function openFolder(doc,k){
  var o=doc.getElementById("div"+k);
  if(o.style.display=="none"){
    o.style.display="block";
  }
  else{
    o.style.display="none";
  }
  replaceImg(doc,k);
  var k=0;
  while((o=o.parentElement)!=doc&&o!=null){
    k=o.id.indexOf("div");
    if(k!=-1){
      if(o.style.display=="none"){
        o.style.display="block";
replaceImg(doc,o.id.substring(k));
      }
    }
  }
}
function clickFolder(doc,k){
	changeColor(doc,k);
	var td=doc.getElementById("td"+k);
	if(navigator.appName=="Microsoft Internet Explorer"){
		var filepath="/"+td.innerText;
	}else{
		var filepath="/"+td.textContent;
	}
	var nodeId=k;
	while(getParentId(nodeId)!=null){
		if(navigator.appName=="Microsoft Internet Explorer"){
			var parentName=document.getElementById("td"+getParentId(nodeId)).innerText;
		}else{
			var parentName=document.getElementById("td"+getParentId(nodeId)).textContent;
		}
		filepath="/"+parentName+filepath;
		nodeId=getParentId(nodeId);
	}
	showOnPreview(filepath);
}
function replaceImg(doc,k){
	var td=doc.getElementById("plus"+k);
	var img=td.firstChild;
	if(img.src.indexOf("plus1.gif")!=-1){
		img.src="tree/win98/minus1.gif";
		return ;
	}
	if(img.src.indexOf("plus2.gif")!=-1){
		img.src="tree/win98/minus2.gif";
		return ;
	}
	if(img.src.indexOf("minus1.gif")!=-1){
		img.src="tree/win98/plus1.gif";
		return ;
	}
	if(img.src.indexOf("minus2.gif")!=-1){
		img.src="tree/win98/plus2.gif";
		return ;
	}
}
function getParentId(k){
	var td=document.getElementById("td"+k);
	//获得包含子目录的div,其id为"div+父目录id".
	var parentDiv=td.parentNode.parentNode.parentNode.parentNode;
	var pid=parentDiv.id;
	if(pid!=null && pid!="undefined" && pid.substring(0,3)=="div"){
//		var parentTd=document.getElementById("td"+pid.substring(3));
//		var parentName=parentTd.innerHTML;
		return pid.substring(3);
	}
	return null;
}
function showOnPreview(filepath){
	var basePath="<%=path%>";
	var newSRC=basePath+"/preview/dlf/"+"CG制作知识库"+filepath;
	display(newSRC);
}
function display(url){
	var child1=top.B.Child;
	var imgTd=child1.document.getElementById("previewImg").parentNode;
	//在预览区要求不改变原图比例不铺张，所以没更新图片的时候重新设置img元素
	imgTd.innerHTML="<img onload='adaptImg(this);' id='previewImg' style='cursor:pointer;' border='1'>";
	var img=imgTd.firstChild;
	if(checkImgExt(url)){
		img.src=url;
	}else if(checkVideoExt(url)){
		img.src="../imagewang/ep.jpg";
	}else if(checkMayaExt(url)){
		img.src="../imagewang/ep2.jpg";
	}else{
		img.src="../imagewang/ep1.jpg";
	}
	
	//IE FF 播放插件不同,IE的播放插件似乎会两次访问url而没有做编解码工作,导致中文路径不能播放
	url=encodeURI(url);
	
	img.onclick=function(){window.open(url,null,"top=150,left=150,scrollbars=yes,dialog=yes,modal=yes,width=750,height=620,resizable=yes");}
}
function checkImgExt(url){
		//这里设置允许的扩展名
		var AllowExt=".jpg|.gif|.jpeg|.png|.bmp";
		var ExtOK=false;
		var ArrayExt;
		if(AllowExt.indexOf('|')!=-1){
			ArrayExt=AllowExt.split('|');
			for(i=0;i<ArrayExt.length;i++){
				if(url.toLowerCase().indexOf(ArrayExt[i])!=-1){
					ExtOK=true;
					break;
				}
			}
		}else{
			ArrayExt=AllowExt;
			if(url.toLowerCase().indexOf(ArrayExt)!=-1){
				ExtOK=true;
			}
		}
	return ExtOK;
}
function checkVideoExt(url){
		//这里设置允许的扩展名
		var AllowExt=".avi|.mov";
		var ExtOK=false;
		var ArrayExt;
		if(AllowExt.indexOf('|')!=-1){
			ArrayExt=AllowExt.split('|');
			for(i=0;i<ArrayExt.length;i++){
				if(url.toLowerCase().indexOf(ArrayExt[i])!=-1){
					ExtOK=true;
					break;
				}
			}
		}else{
			ArrayExt=AllowExt;
			if(url.toLowerCase().indexOf(ArrayExt)!=-1){
				ExtOK=true;
			}
		}
	return ExtOK;
}
function checkMayaExt(url){
		//这里设置允许的扩展名
		var AllowExt=".ma|.mb";
		var ExtOK=false;
		var ArrayExt;
		if(AllowExt.indexOf('|')!=-1){
			ArrayExt=AllowExt.split('|');
			for(i=0;i<ArrayExt.length;i++){
				if(url.toLowerCase().indexOf(ArrayExt[i])!=-1){
					ExtOK=true;
					break;
				}
			}
		}else{
			ArrayExt=AllowExt;
			if(url.toLowerCase().indexOf(ArrayExt)!=-1){
				ExtOK=true;
			}
		}
	return ExtOK;
}
</script>
</head>

<body>
<%
out.flush();
File f=new File((String)request.getAttribute("filebase"));
Tree t=Tree.getTree(f,new FileContainer());
paintChilds(t.getChilds(),response.getWriter());
%>
<form id="thisForm" name="thisForm">
  <input type="hidden" name="selectedNode" id="selectedNode">
  <input type="hidden" name="bgColor" id="bgColor" value="#FFFFFF">
  <input type="hidden" name="selectedColor" id="selectedColor" value="#9999FF">
</form>
</body>
</html>